package com.example.treelistviewdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.example.treelistviewdemo.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;

    MainViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        viewModel = new ViewModelProvider(this).get(MainViewModel.class);
        viewModel.create();

        viewModel.nodeAdapter = new MainActivity.NodeAdapter();
        binding.listView.setAdapter(viewModel.nodeAdapter);

        viewModel.nodes.observe(this, new Observer<List<Node>>() {
            @Override
            public void onChanged(List<Node> nodes) {
                List<Node> newArray = new ArrayList<>();
                for (Node child : nodes) {
                    newArray.add(child.copy());
                }
                viewModel.nodeAdapter.submitList(newArray);
            }
        });
    }


    public class NodeAdapter extends ListAdapter<Node, NodeAdapter.ViewHolder> {

        private static final DiffUtil.ItemCallback<Node> CALLBACK = new DiffUtil.ItemCallback<>() {
            @Override
            public boolean areItemsTheSame(@NonNull Node oldItem, @NonNull Node newItem) {
                return oldItem.id.equals(newItem.id);
            }

            @Override
            public boolean areContentsTheSame(@NonNull Node oldItem, @NonNull Node newItem) {
                return oldItem.id.equals(newItem.id) && oldItem.selection.equals(newItem.selection) && oldItem.fold == newItem.fold;
            }
        };

        protected NodeAdapter() {
            super(CALLBACK);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_view, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Node child = getItem(position);

            ImageView imageView = holder.itemView.findViewById(R.id.selection_icon);
            if (child.selection == SelectionStatus.none) {
                imageView.setImageResource(R.drawable.baseline_check_box_outline_blank_24);
            } else if (child.selection == SelectionStatus.any) {
                imageView.setImageResource(R.drawable.baseline_indeterminate_check_box_24);
            } else if (child.selection == SelectionStatus.all) {
                imageView.setImageResource(R.drawable.baseline_check_box_24);
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Node child = getItem(holder.getAdapterPosition());
                    viewModel.selectionAction(child);
                }
            });

            ImageView arrow = holder.itemView.findViewById(R.id.selection_arrow);
            arrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Node child = getItem(holder.getAdapterPosition());
                    viewModel.itemSelectAction(child);
                }
            });
            if (child.children != null) {
                arrow.setVisibility(View.VISIBLE);
                if (child.fold) {
                    arrow.setImageResource(R.drawable.baseline_keyboard_arrow_down_24);
                } else {
                    arrow.setImageResource(R.drawable.baseline_keyboard_arrow_up_24);
                }
            } else {
                arrow.setVisibility(View.GONE);
            }

            float density = getResources().getDisplayMetrics().density;
            int marginInPx = (int) (30 * density);
            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) imageView.getLayoutParams();
            layoutParams.setMarginStart(child.level * marginInPx);
            imageView.setLayoutParams(layoutParams);


            TextView textView = holder.itemView.findViewById(R.id.selection_text_view);
            textView.setText(child.title);

        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }
}